package cn.sdormitory.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.sdormitory.attendance.dao.AttendanceRulesDao;
import cn.sdormitory.attendance.entity.AttendanceRules;
import cn.sdormitory.attendance.vo.AttendanceRuleTimeSlot;
import cn.sdormitory.basedata.dao.BClassDao;
import cn.sdormitory.basedata.dao.BStudentDao;
import cn.sdormitory.basedata.entity.BClass;
import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.basedata.service.BClassService;
import cn.sdormitory.basedata.vo.*;
import cn.sdormitory.common.IdAndName;
import cn.sdormitory.common.utils.ContainerUtil;
import cn.sdormitory.common.utils.StringUtils;
import cn.sdormitory.common.utils.SysUserUtils;
import cn.sdormitory.service.AppClassService;
import cn.sdormitory.smartdor.dao.SdAttenceDao;
import cn.sdormitory.smartdor.dao.SdWeekendAttenceDao;
import cn.sdormitory.sys.entity.SysUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @创建人：zhouyang
 * @创建时间：2021/11/10 17:49
 * @version：V1.0
 */
@Service
public class AppClassServiceImpl extends ServiceImpl<BClassDao, BClass> implements AppClassService {
    @Resource
    private BClassDao bClassDao;

    @Resource
    private BClassService bClassService;

    @Autowired
    AttendanceRulesDao attendanceRulesDao;

    @Resource
    BStudentDao bStudentDao;

    @Autowired
    SdAttenceDao sdAttenceDao;

    @Resource
    SdWeekendAttenceDao sdWeekendAttenceDao;

    @Override
    public List<IdAndName<Long>> getIdAndNames() {
        return bClassService.getIdAndNames();
    }

    @Override
    public List<BClassVo> getAppClassListByTeaId(Long loginId) {
        List<BClassVo> classList = bClassDao.getAppClassListByTeaId(loginId);
        List<BClassVo> validClass = new ArrayList<>();
        for (int i = 0; i < classList.size(); i++) {
            LambdaQueryWrapper<BStudent> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BStudent::getClassId,classList.get(i).getClassId())
                    .eq(BStudent::getIsGraduated,"0");
            List<BStudent> studentList = bStudentDao.selectList(wrapper);
            if(studentList.size() > 0){
                validClass.add(classList.get(i));
            }
        }
        return validClass;
//        return bClassDao.getAppClassListByTeaId(loginId);
    }

    @Override
    public List<BClassStudentVo> getAppClassStuListByCName(String className) {
        return bClassDao.getAppClassStuListByCName(className);
    }

    @Override
    public List<BClassDorVo> getAppClassDorListByTeaId(Long classTeacherId) {
        return bClassDao.getAppClassDorListByTeaId(classTeacherId);
    }

    @Override
    public List<ClassDeptVo> getAppClassDept() {
        return bClassDao.getAppClassDept();
    }

    @Override
    public List<BClassVo> getAppClassDeptByClaId(Long classDeptId) {
        return bClassDao.getAppClassDeptByClaId(classDeptId);
    }

    @Override
    public List<BClassDeptVo> getAppClassDeptAccDvByDeptId(String accessDate, Long classDeptId,String timeSlot) {
        return bClassDao.getAppClassDeptAccDvByDeptId(accessDate,classDeptId,timeSlot);
    }

    @Override
    public List<BClassDeptHyVo> getAppClassDeptHyByDeptId(String accessDate, Long classDeptId,String timeSlot) {
        return bClassDao.getAppClassDeptHyByDeptId(accessDate,classDeptId,timeSlot);
    }

    @Override
    public List<BClassDeptReVo> getAppClassDeptReByDeptId(Long classDeptId) {
        return bClassDao.getAppClassDeptReByDeptId(classDeptId);
    }

    @Override
    public List<BClassVo> getAllAppClassList() {
        return bClassDao.getAllAppClassList();
    }

    @Override
    public List<BClassStudentVo> getClassStuListByClassId(String classId) {
        return bClassDao.getClassStuListByClassId(classId);
    }

    @Override
    public List<BClassStudentVo> getClassStuListAllByClassId(String classId) {
        return bClassDao.getClassStuListAllByClassId(classId);
    }

    @Override
    public Integer getBoardingStuCountByClassId(String classId) {
        return bClassDao.getBoardingStuCountByClassId(classId);
    }

    @Override
    public Integer getLeaveStuCountByClassId(String classId) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return bClassDao.getLeaveStuCountByClassId(classId,format.format(new Date()));
    }

    @Override
    public BClass getClassInfoByClassId(Long classId) {
        BClass classInfo = bClassDao.getClassInfoByClassId(classId);
        List<AttendanceRules> rulesList = new ArrayList<>();

        if(classInfo.getClassAttendanceRules() != null){
            String[] ruleIds = classInfo.getClassAttendanceRules().split(",");
            for (int i = 0; i < ruleIds.length; i++) {
                AttendanceRules rule = attendanceRulesDao.getAttendanceRulesDetailInfoById(Long.valueOf(ruleIds[i]));
                rulesList.add(rule);
            }
            Collections.sort(rulesList, Comparator.comparing(AttendanceRules::getAttendanceStartTime));
            classInfo.setRulesList(rulesList);
        }

        return classInfo;
    }

    @Override
    public int UpdateClassInfo(BClass bClass) {
        return bClassDao.updateById(bClass);
    }
    @Override
    public List<BStudentListVo> getStudentListByClassId(int classId){
        return bClassDao.getStudentListByClassId(classId);
    }
    @Override
    public int updateStudent(int id,String studentName,String studentPhone,String parentPhone){
        return bClassDao.updateStudent(id,studentName,studentPhone,parentPhone);
    }

    @Override
    public List<BStudentListVo> getWeekendNotClockInListByCId(Long classId, String checkDate) {
//        Date date = new Date();
//        String accessDate = DateUtils.formatDate(date, "yyyy-MM-dd");
        List<BStudentListVo> list = sdWeekendAttenceDao.getWeekendClockInStudentByCId(classId, checkDate);
        return list;
    }

    @Override
    public List<BStudentListVo> getDormitoryNotClockInListByCId(Long classId, String checkDate) {
//        Date date = new Date();
//        String accessDate = DateUtils.formatDate(date, "yyyy-MM-dd");
        List<BStudentListVo> list = sdAttenceDao.getDormitoryUnclcokListByClassId(classId, checkDate);
        return list;
    }

    @Override
    public List<Long> getDorIdsByClassId(Long classId) {
        return bClassDao.getDorIdsByClassId(classId);
    }

    @Override
    public List<BClassVo> getAppClassByDeptIds(Collection<Long> deptIds) {
        return bClassDao.getAppClassByDeptIds(deptIds);
    }

    @Override
    public List<BClassVo> getAppClassByDeptManager() {
        Assert.isTrue(SysUserUtils.isDeptManager(), "当前用户不是部门经理");
        Long deptId = SysUserUtils.getSysUser().getDeptId();
        Assert.notNull(deptId,"当前用户没有部门");
        return getAppClassByDeptId(deptId);
    }

    @Override
    public List<AttendanceRuleTimeSlot> getClassRuleTimeSlots(Long classId) {
        String attendanceRules = getObj(Wrappers.lambdaQuery(BClass.class)
                .select(BClass::getClassAttendanceRules)
                .eq(BClass::getId, classId), Convert::toStr);
        if(StringUtils.isBlank(attendanceRules)){
            return Collections.emptyList();
        }
        List<Long> ruleIds = ContainerUtil.splitToIds(attendanceRules);
        if(ContainerUtil.isEmpty(ruleIds)){
            return Collections.emptyList();
        }
        List<Object> timeSlots = attendanceRulesDao.selectObjs(Wrappers.lambdaQuery(AttendanceRules.class)
                .select(AttendanceRules::getTimeSlot)
                .in(AttendanceRules::getId, ruleIds)
                .orderByAsc(AttendanceRules::getAttendanceStartTime)
        );
        return ContainerUtil.mapList(timeSlots,timeSlot->new AttendanceRuleTimeSlot(Convert.toStr(timeSlot)));
    }

}
